﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="Cooking.Web.Views.Recipe.Edit" %>
<%@ Import Namespace="Cooking.Web.Helpers"%>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Model.Recipe.Name %></h2>
    
    <span id="ingredientList"><% Html.RenderPartial("IngredientList", Model); %></span>
    
    
    <fieldset>
        <legend>Steps</legend>
            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="data-table">
                <tr>
                    <th scope="col">Number</th>
                    <th scope="col">Description</th>
                    <th></th>
                </tr>
                
                <%
                foreach (var step in Model.Recipe.Steps)
                {
                    if (step.Id != Model.EditStepId)
                    {
                        var arguments = new {RecipeId = Model.Recipe.Id, StepId = step.Id};
                        %>
                        <tr>
                            <td><%= step.SequenceNumber %></td>
                            <td><%= step.Description %></td>
                            <td>
                                <%= Html.ActionLinkIf(Model.AllowAddSteps(), "Edit", "EditStep", arguments) %>
                                <%= Html.LiteralIf(Model.AllowAddSteps(), " | ") %>
                                <%= Html.ActionLinkIf(Model.AllowAddSteps(), "Delete", "DeleteStep", arguments)%>
                                <%= Html.ActionLinkIf(Model.AllowAddSteps(), "Move Up", "MoveStepUp", arguments) %>
                                <%= Html.ActionLinkIf(Model.AllowAddSteps(), "Move Down", "MoveStepDown", arguments) %>
                            </td>
                        </tr>
                        <%
                    }
                    else
                    {
                        Html.BeginForm("UpdateStep", "Recipe");
                        %>
                            <tr>
                                <td><%= step.SequenceNumber %></td>
                                <td><%= Html.TextArea("description", step.Description, new { Rows = 5, Cols = 100 })%></td>
                                <td>
                                    <%=Html.Hidden("Recipeid", Model.Recipe.Id) %>
                                    <%=Html.Hidden("StepId", step.Id) %>
                                    <input class="button button-big" name="Submit" type="submit" value="Save"  />
                                </td>
                            </tr>
                        <%
                        Html.EndForm();
                    }
                    
                } 
                if (Model.AllowAddSteps())
                {
                    Html.BeginForm("AddStep", "Recipe");
                    %>
                        <tr>
                            <td><%= Model.Recipe.Steps.Count + 1 %></td>
                            <td><%= Html.TextArea("description", new { Rows = 5, Cols = 100 }) %></td>
                            <td>
                                <%=Html.Hidden("Recipeid", Model.Recipe.Id) %>
                                <input class="button button-big" name="Submit" type="submit" value="Add"  />
                            </td>
                        </tr>
                    <%
                    Html.EndForm();
                }
                %>
                
            </table>
        </legend>
    </fieldset>
    
    
    <script type="text/javascript">
        function AddIngredient() {
            var recipeId = $("#addDto\\.RecipeId").val();
            var name = $("#addDto\\.IngredientName").val();
            var qty = $("#addDto\\.Quantity").val();
            var uom = $("#addDto\\.UnitOfMeasure").val();
            var prep = $("#addDto\\.IngredientPrep").val();

            $.ajax({
                type: "POST",
                url: "/Recipe/AddIngredientAjax",
                data: "addDto.RecipeId=" + recipeId + "&addDto.IngredientName=" + name + "&addDto.Quantity=" + qty + "&addDto.UnitOfMeasure=" + uom + "&addDto.IngredientPrep=" + prep,
                dataType: "html",
                success: function(result) {
                    var domElement = $(result); // create element from html
                    $("#ingredientList").empty();
                    $("#ingredientList").append(domElement);
                }
            });
        }
    </script>

    
</asp:Content>
